#include<bits/stdc++.h>

using namespace std;

int main()
{
	int t;
    cin>>t;
    int mod=1e9+7;
    while(t--)
	{
        long long n,k,j=1;
        long long ans=0;
        cin>>n>>k;
        while(k)
		{
           if(k%2==1)
		   {
             ans=(ans+j)%mod;
           }
           k/=2;
           j=(j*n)%mod;
        }
        cout<<ans<<endl;
    }
    return 0;
} 
